package self.example.explore.config;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import lombok.extern.slf4j.Slf4j;
import self.example.explore.util.NonPoolDataSourceInfoParser;

import java.util.Collections;
import java.util.Objects;

import static self.example.explore.constant.DataSourceConfigInfo.*;

/**
 * @author 1.
 * @version 1.0
 * @description: Mybatis-plus generator 代码自动生成器
 * @date 2023/4/17 16:33
 */
@Slf4j
public class MpGeneratorConfigur {
    public static void main(String[] args) {
//        System.out.println(System.getProperty("user.dir").replaceAll("\\\\", "/")); //C:\develop\ready-work\colleges\colleges
//        System.out.println(System.getProperty("user.dir")); //C:\develop\ready-work\colleges\colleges

        // 当前工程根目录
        final String ENGINEER_URL = System.getProperty("user.dir").replaceAll("\\\\","/");
        // 当前模块
        final String MODULE_URL = "/mybatis-plus-generator-play";
        // 当前项目源码
        final String ROOT_URL = "/src/main/java";
        // 父包名
        final String PARENT_DIR = "self.example.explore";
        // 当前工程项目绝对路径(模块)
        final String ROOT_MODULE_URL = ENGINEER_URL + MODULE_URL;
        // 当前工程项目源码所在路径
        final String PROJECT_ROOT_URL = ROOT_MODULE_URL + ROOT_URL;
        // xml 文件路径
        final String XML_URL = ROOT_MODULE_URL + "/src/main/resources/mapper/";
        //如果模板引擎是 velocity
        final String VELOCITY_TEMPLATE_PATH = "/templates/mapper.xml.vm";
        // 如果模板引擎是 freemarker
        final String FREEMARKER_TEMPLATE_PATH = "/templates/mapper.xml.ftl";

        // 获取数据源信息解析器
        NonPoolDataSourceInfoParser parser = new NonPoolDataSourceInfoParser();

        /// 代码生成器
        FastAutoGenerator.create(URL, USER_NAME, PASS_WORD)
                .globalConfig(builder -> {
                    builder.author("1.")
                            .commentDate("yyyy-MM-dd")
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(PROJECT_ROOT_URL);
                })
                .packageConfig(builder -> {
                    builder.parent(PARENT_DIR)
                            .entity("po")
                            .service("bo")
                            .serviceImpl("bo.impl")
                            .mapper("dao") //映射类
                            .xml("")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, XML_URL));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(Objects.requireNonNull(parser.getTableNames()))
                            .enableCapitalMode();
                    //Mapper 策略配置
                    builder.mapperBuilder()
                            .enableFileOverride() //生成文件时覆盖原有文件
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sXml");
                    //Entity 策略配置
                    builder.entityBuilder()
                            .enableFileOverride()
                            .enableChainModel() //开启链式模型
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .addTableFills(new Column("create_time", FieldFill.INSERT),
                                    new Column("storage_time",FieldFill.INSERT));
                    //Service 策略配置
                    builder.serviceBuilder()
                            .enableFileOverride()
                            .superServiceClass(IService.class) //服务接口父接口
                            .superServiceImplClass(ServiceImpl.class); //服务实现类父类
                })
                .templateConfig(builder -> {
                    builder.disable(TemplateType.CONTROLLER);
                })
                .execute();


    }
}
